package cn.xspace.library.util;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.been.AppIndexInfo;
import com.xspace.android.xafcommon.been.AppInfoBean;
import com.xspace.android.xafcommon.been.BUserSignInfo;
import com.xspace.android.xafcommon.been.Equipment;
import com.xspace.android.xafcommon.been.QueryInitialDataBean;
import com.xspace.android.xafcommon.been.UserAuthorityBean;
import com.xspace.android.xafcommon.been.UserInfoBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.LogUtil;
import com.xspace.android.xafcommon.util.PreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class MyInfoUtil {
    /**
     * 查看部门人员日志
     **/
    public static final String ViewDeptuserWorklog = "view_deptuser_worklog";
    // public static final String ViewDeptuserWorklog="view_log_branch";
    /**
     * 查看监管统计日志
     **/
    public static final String ViewJianguanStatistic = "view_jianguan_statistic";
    // public static final String ViewJianguanStatistic="view_log_tongji";
    /**
     * 查看直属单位日志
     **/
    public static final String ViewDirdeptWorklog = "view_dirdept_worklog";
    // public static final String ViewDirdeptWorklog="view_log_dirdept";
    /**
     * 专业群组/圈子聊天
     **/
    public static final String ChatWithGroupUser = "chat_with_groupuser";
    /**
     * 本机构聊天
     **/
    public static final String ChatWithSelfDept = "chat_with_selfdept";
    /**
     * 上级直属机构聊天
     **/
    public static final String ChatWithUpDirDept = "chat_with_updirdept";
    /**
     * 上级归口机构聊天
     **/
    public static final String ChatWithUpBelongDept = "chat_with_upbelongdept";
    /**
     * 同级直属机构聊天
     **/
    public static final String ChatWithEquivDirDept = "chat_with_equivdirdept";
    /**
     * 下级直属机构聊天
     **/
    public static final String ChatWithDirDept = "chat_with_dirdept";
    /**
     * 下级归口机构聊天
     */
    public static final String ChatWithBelong = "chat_with_belongdept";
    /**
     * 璧山特殊权限(APP机构和直属)
     **/
    public static final String BiShanAppOrgAndDirect = "chat_with_app_dept_dir";
    /**
     * 与服务对象聊天
     **/
    public static final String ChatWithServiceObj = "chat_with_serviceobj";
    /**
     * 工作日志归口权限
     */
    public static final String WorklogBelong = "view_belongdept_worklog";
    /**
     * 视频监控权限
     */
    public static final String ViewMonitorApp = "view_monitor_app";

    /**
     * 农民端专用,是否有检疫权限
     */
    public static final String JianYiForm = "jinayi_from";

    //    public static Map<String, UserAuthorityBean> authorityMap = new HashMap<String, UserAuthorityBean>();
//    public static Map<String, UserAuthorityBean> authorityMapOrg = new HashMap<String, UserAuthorityBean>();
    public static Map<String, Equipment> equipmentMap = new HashMap<String, Equipment>();
    public static UserInfoBean userInfoBean;
    public static AppInfoBean appInfoBean;
    public static AppIndexInfo appIndexBean;

    public static String imei = Tools.getImei(BaseApplication.mInstance);

    public static void setUserAppInfo(QueryInitialDataBean bean) {
        //设置用户信息
        if (bean == null)
            return;
        if (bean.user != null) {
            setUserInfo(bean.user);
        }
        if (bean.app != null) {
            setAppBeanInfo(bean.app);
        }
        if (bean.appIndex != null) {
            setAppIndexInfo(bean.appIndex);
        }
        if (bean.authList != null) {
            setUserAuthorityData(bean.authList);
        }
        if (bean.equList != null) {
            setUserEquData(bean.equList);
        }
    }

    /**
     * 设置accessToken和其他数据
     *
     * @param info
     */
    private static void setUserInfo(UserInfoBean info) {
        LogUtil.d("wubaojie>>>setUserInfo: " + (info == null));
        if (info == null)
            return;
        if (!TextUtils.isEmpty(info.user_id)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_id = info.user_id;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ID, info.user_id);
        }
        if (!TextUtils.isEmpty(info.user_name)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_name = info.user_name;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_NAME_KEY, info.user_name);
        }
        if (!TextUtils.isEmpty(info.user_mobile)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_mobile = info.user_mobile;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_MOBILE, info.user_mobile);
        }
        if (!TextUtils.isEmpty(info.user_logo)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_logo = info.user_logo;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_LOGO, info.user_logo);
        }
        if (!TextUtils.isEmpty(info.user_address)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_address = info.user_address;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ADDRESS, info.user_address);
        }
        if (!TextUtils.isEmpty(info.user_sex)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_sex = info.user_sex;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_SEX, info.user_sex);
        }
        if (!TextUtils.isEmpty(info.user_email)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_email = info.user_email;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_EMAIL, info.user_email);
        }
        if (!TextUtils.isEmpty(info.user_desc)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_desc = info.user_desc;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_DESC, info.user_desc);
        }
        if (!TextUtils.isEmpty(info.user_bjid)) { // TODO 登录tigase使用，后期会去掉
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_bjid = info.user_bjid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_BAREJID_KEY, info.user_bjid);
        }
        if (!TextUtils.isEmpty(info.user_areacode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_areacode = info.user_areacode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_AREACODE, info.user_areacode);
        }
        if (!TextUtils.isEmpty(info.user_score)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_score = info.user_score;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_SCORE, info.user_score);
        }

        if (!TextUtils.isEmpty(info.user_orgcode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgcode = info.user_orgcode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ORGCODE, info.user_orgcode);
        }
        if (!TextUtils.isEmpty(info.user_org_areacode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_org_areacode = info
                    .user_org_areacode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ORG_AREACODE, info.user_org_areacode);
        }
        if (!TextUtils.isEmpty(info.user_qacexpert)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_qacexpert = info.user_qacexpert;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_QACEXPERT, info.user_qacexpert);
        }
        //未变动
        if (!TextUtils.isEmpty(info.user_deptid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptid = info.user_deptid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_DEPTID, info.user_deptid);
        }
        if (!TextUtils.isEmpty(info.user_deptname)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptname = info.user_deptname;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_DEPTNAME, info.user_deptname);
        }
        if (!TextUtils.isEmpty(info.user_orgid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgid = info.user_orgid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ORGTID, info.user_orgid);
        }
        if (!TextUtils.isEmpty(info.user_orgname)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgname = info.user_orgname;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ORGNAME, info.user_orgname);
        }
        if (!TextUtils.isEmpty(info.user_deptcode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptcode = info.user_deptcode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_DEPTCODE, info.user_deptcode);
        }
        if (!TextUtils.isEmpty(info.user_qq)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_qq = info.user_qq;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_QQ, info.user_qq);
        }
        if (!TextUtils.isEmpty(info.user_dept_areacode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_dept_areacode = info
                    .user_dept_areacode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_DEPT_AREACODE, info.user_dept_areacode);
        }
        if (!TextUtils.isEmpty(info.user_orghxid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orghxid = info.user_orghxid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ORG_HXID, info.user_orghxid);
        }
        if (!TextUtils.isEmpty(info.user_isfarmer)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_isfarmer = info.user_isfarmer;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ISFARMER, info.user_isfarmer);
        }

//        if (!TextUtils.isEmpty(info.uuid)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.uuid = info.uuid;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ID, info.uuid);
//        }
//        if (!TextUtils.isEmpty(info.name)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.name = info.name;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_NAME_KEY, info.name);
//        }
//        if (!TextUtils.isEmpty(info.mobile)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.mobile = info.mobile;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_MOBILE, info.mobile);
//        }
//        if (!TextUtils.isEmpty(info.logo)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.logo = info.logo;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_LOGO, info.logo);
//        }
//        if (!TextUtils.isEmpty(info.address)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.address = info.address;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_ADDRESS, info.address);
//        }
//        if (!TextUtils.isEmpty(info.sex)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.sex = info.sex;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_SEX, info.sex);
//        }
//        if (!TextUtils.isEmpty(info.email)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.email = info.email;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_EMAIL, info.email);
//        }

//        if (!TextUtils.isEmpty(info.area_code)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.area_code = info.area_code;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_AREACODE, info.area_code);
//        }
//        if (!TextUtils.isEmpty(info.score)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.score = info.score;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_SCORE, info.score);
//        }
//        if (!TextUtils.isEmpty(info.qq)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.qq = info.qq;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_USER_QQ, info.qq);
//        }
//        if (!TextUtils.isEmpty(info.qac_expert)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.qac_expert = info.qac_expert;
//            PreferencesUtils.setPfValue(Constants.QAC_USER_POWER, info.qac_expert);
//        }

        if (info.bizList != null) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.bizList = info.bizList;
            Gson gson = new Gson();
            String bizlist = gson.toJson(info.bizList);
            PreferencesUtils.setPfValue(Constants.ACCOUNT_BIZLIST, bizlist);
        }

        if (!TextUtils.isEmpty(info.access_token)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.access_token = info.access_token;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_Acccess_Token, info.access_token);
        }
        if (!TextUtils.isEmpty(info.user_zhencode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_zhencode = info.user_zhencode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_ZhenCode, info.user_zhencode);
        }
        if (!TextUtils.isEmpty(info.user_zhenname)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_zhenname = info.user_zhenname;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_Zhen_Name, info.user_zhenname);
        }
        if (!TextUtils.isEmpty(info.user_cuncode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_cuncode = info.user_cuncode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_CunCode, info.user_cuncode);
        }
        if (!TextUtils.isEmpty(info.user_cunname)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_cunname = info.user_cunname;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_CunName, info.user_cunname);
        }
        if (!TextUtils.isEmpty(info.user_sheid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_sheid = info.user_sheid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_SheId, info.user_sheid);
        }
        if (!TextUtils.isEmpty(info.user_shename)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_shename = info.user_shename;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_ShenName, info.user_shename);
        }
    }

    private static void setAppBeanInfo(AppInfoBean info) {
        if (!TextUtils.isEmpty(info.app_deptid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_deptid = info.app_deptid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_ORGID, info.app_deptid);
        }
        if (!TextUtils.isEmpty(info.app_dept_areacode)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_dept_areacode = info.app_dept_areacode;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_ORG_AREACODE, info.app_dept_areacode);
        }
        if (!TextUtils.isEmpty(info.app_orghxid)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_orghxid = info.app_orghxid;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_ORG_HXID, info.app_orghxid);
        }
        String app_version = BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_version;
        if (!TextUtils.isEmpty(app_version)) {
            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_VERSION, app_version);
        }
    }

    private static void setAppIndexInfo(AppIndexInfo info) {
//        if (!TextUtils.isEmpty(info.app_url)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).appIndexInfo.app_url = info.app_url;
//            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_INDEX, info.app_url);
//        }
//        if (!TextUtils.isEmpty(info.version_number)) {
//            BuildCache.getAppConfigBean(BaseApplication.mInstance).appIndexInfo.version_number = info.version_number;
//            PreferencesUtils.setPfValue(Constants.HTML_VERSION, info.version_number);
//        }
        //新接口数据
        if (!TextUtils.isEmpty(info.app_index)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).appIndexInfo.app_index = info.app_index;
            PreferencesUtils.setPfValue(Constants.ACCOUNT_APP_INDEX, info.app_index);
        }
        if (!TextUtils.isEmpty(info.app_versionnum)) {
            BuildCache.getAppConfigBean(BaseApplication.mInstance).appIndexInfo.app_versionnum = info.app_versionnum;
            PreferencesUtils.setPfValue(Constants.HTML_VERSION, info.app_versionnum);
        }
    }

    private static void setUserAuthorityData(List<UserAuthorityBean> authList) {
        if (authList != null && authList.size() > 0) {
            //保存 权限json字符串
            String authListString = new Gson().toJson(authList);
            PreferencesUtils.setPfValue(Constants.ACCOUNT_User_Authority_Jsondata, authListString);
        }
        setAuthMap(authList);
    }

    public static void setAuthMap(List<UserAuthorityBean> authList) {
        BuildCache.mBuildConfig.authorityMap.clear();
        for (int i = 0; i < authList.size(); i++) {
            UserAuthorityBean tuab = authList.get(i);
            BuildCache.mBuildConfig.authorityMap.put(tuab.fun_key, tuab);
        }
    }

    private static void setUserEquData(List<Equipment> equList) {
        if (equList != null && equList.size() > 0) {
            //保存 权限json字符串
            String equListString = new Gson().toJson(equList);
            PreferencesUtils.setPfValue(Constants.ACCOUNT_EQUIPMENTS, equListString);
        }
        setEquMap(equList);
    }

    public static void setEquMap(List<Equipment> equList) {
        equipmentMap.clear();
        if (equList != null) {
            for (Equipment equipment : equList) {
                equipmentMap.put(equipment.imei, equipment);
            }
        }
    }

    public static String getMyUserid(Context context) {
        String user_id = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_id;
        if (TextUtils.isEmpty(user_id)) {
            user_id = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
        }
        return user_id;
    }

    public static String getMyUserId() {
        String user_id = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_id;
        if (TextUtils.isEmpty(user_id)) {
            user_id = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
        }
        return user_id;
    }

    public static String getName(Context context) {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_name;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_NAME_KEY);
        }
        return token;
    }

    public static String getMyMobile(Context context) {
        String user_mobile = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_mobile;
        if (TextUtils.isEmpty(user_mobile)) {
            user_mobile = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_MOBILE);
        }
        return user_mobile;
    }

    public static String getMyLogo(Context context) {
        String user_logo = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_logo;
        if (TextUtils.isEmpty(user_logo)) {
            user_logo = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_LOGO);
        }
        return user_logo;
    }

    public static String getMyBareJid() {
        String jid = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_bjid;
        if (TextUtils.isEmpty(jid)) {
            jid = PreferencesUtils.getPfValue(Constants.ACCOUNT_BAREJID_KEY);
        }
        return jid;
    }

    /**
     * 获取用户机构id
     **/
    public static String getUseOrgId(Context context) {
        String user_orgid = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgid;
        if (TextUtils.isEmpty(user_orgid)) {
            user_orgid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORGTID);
        }
        return user_orgid;
    }

    public static String getUserOrgId() {
        String user_orgid = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgid;
        if (TextUtils.isEmpty(user_orgid)) {
            user_orgid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORGTID);
        }
        return user_orgid;
    }

    public static String getUserSex() {
        String userSex = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_sex;
        if (TextUtils.isEmpty(userSex)) {
            userSex = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_SEX);
        }
        return userSex;
    }

    /**
     * 获取用户所在部门的deptid
     **/
    public static String getUserDeptId() {
        String deptid = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptid;
        if (TextUtils.isEmpty(deptid)) {
            deptid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTID);
        }
        return deptid;
    }

    public static String getUserOrgName() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_orgname;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORGNAME);
        }
        return token;
    }

    /**
     * 本机构Id 由配置文件读出来
     *
     * @return
     */
    public static String getAppOrgId() {
        String orgid = BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_deptid;
        if (TextUtils.isEmpty(orgid)) {
            orgid = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORGID);
        }
        return orgid;
    }

    public static String getPlatCode() {
        return BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_platcode;
    }

    public static String getUserAreaCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_areacode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_AREACODE);
        }
        return token;
    }

    public static String getUserOrgAreaCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_org_areacode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORG_AREACODE);
        }
        return token;
    }

    public static String getUserAuthList() {
        return PreferencesUtils.getPfValue(Constants.ACCOUNT_User_Authority_Jsondata);
    }


    /**
     * 获取用户所在app机构名称
     **/
    public static String getDeptName() {
        String dept_name = BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_deptname;
        return dept_name;
    }

    public static String getUserAuthorityData() {
        return PreferencesUtils.getPfValue(Constants.ACCOUNT_User_Authority_Jsondata);
    }

    public static String getAccessToken() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.access_token;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_Acccess_Token);
        }
        return token;
    }

    /**
     * 用户所在部门
     **/
    public static String getUserDeptName() {
//        String token = AppCache.getAppConfigBean(BaseApplication.mInstance).user_deptname;
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptname;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTNAME);
        }
        return token;
    }

    public static String getIsFarmer() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_isfarmer;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ISFARMER);
        }
        return token;
    }

    public static String getUserDeptCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_deptcode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTCODE);
        }
        return token;
    }

    public static String getUserDeptAreacode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_dept_areacode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPT_AREACODE);
        }
        return token;
    }

    public static String getAppDeptAreaCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).appInfo.app_dept_areacode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORG_AREACODE);
        }
        return token;
    }


//    public static UserInfoBean getUserInfo(Context context) {
//        if (userInfoBean != null)
//            return userInfoBean;
//        userInfoBean = new UserInfoBean();
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.mobile)) {
//            userInfoBean.mobile = BuildCache.getAppConfigBean(context).userInfoBean.mobile;
//        } else {
//            userInfoBean.mobile = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_MOBILE);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.jid)) {
//            userInfoBean.jid = BuildCache.getAppConfigBean(context).userInfoBean.jid;
//        } else {
//            userInfoBean.jid = PreferencesUtils.getPfValue(Constants.ACCOUNT_BAREJID_KEY);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.address)) {
//            userInfoBean.address = BuildCache.getAppConfigBean(context).userInfoBean.address;
//        } else {
//            userInfoBean.address = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ADDRESS);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.logo)) {
//            userInfoBean.logo = BuildCache.getAppConfigBean(context).userInfoBean.logo;
//        } else {
//            userInfoBean.logo = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_LOGO);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.isfarmer)) {
//            userInfoBean.isfarmer = BuildCache.getAppConfigBean(context).userInfoBean.isfarmer;
//        } else {
//            userInfoBean.isfarmer = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ISFARMER);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.email)) {
//            userInfoBean.email = BuildCache.getAppConfigBean(context).userInfoBean.email;
//        } else {
//            userInfoBean.email = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_EMAIL);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.qac_expert)) {
//            userInfoBean.qac_expert = BuildCache.getAppConfigBean(context).userInfoBean.qac_expert;
//        } else {
//            userInfoBean.qac_expert = PreferencesUtils.getPfValue(Constants.QAC_USER_POWER);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_deptid)) {
//            userInfoBean.user_deptid = BuildCache.getAppConfigBean(context).userInfoBean.user_deptid;
//        } else {
//            userInfoBean.user_deptid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTID);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_deptname)) {
//            userInfoBean.user_deptname = BuildCache.getAppConfigBean(context).userInfoBean.user_deptname;
//        } else {
//            userInfoBean.user_deptname = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTNAME);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_dept_areacode)) {
//            userInfoBean.user_dept_areacode = BuildCache.getAppConfigBean(context).userInfoBean.user_dept_areacode;
//        } else {
//            userInfoBean.user_dept_areacode = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPT_AREACODE);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_orgid)) {
//            userInfoBean.user_orgid = BuildCache.getAppConfigBean(context).userInfoBean.user_orgid;
//        } else {
//            userInfoBean.user_orgid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORGTID);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_orghxid)) {
//            userInfoBean.user_orghxid = BuildCache.getAppConfigBean(context).userInfoBean.user_orghxid;
//        } else {
//            userInfoBean.user_orghxid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORG_HXID);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_orgname)) {
//            userInfoBean.user_orgname = BuildCache.getAppConfigBean(context).userInfoBean.user_orgname;
//        } else {
//            userInfoBean.user_orgname = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ORGNAME);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_deptcode)) {
//            userInfoBean.user_deptcode = BuildCache.getAppConfigBean(context).userInfoBean.user_deptcode;
//        } else {
//            userInfoBean.user_deptcode = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTCODE);
//        }
//
////        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.uuid)) {
////            userInfoBean.uuid = BuildCache.getAppConfigBean(context).userInfoBean.uuid;
////        } else {
////            userInfoBean.uuid = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
////        }
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_id)) {
//            userInfoBean.user_id = BuildCache.getAppConfigBean(context).userInfoBean.user_id;
//        } else {
//            userInfoBean.user_id = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_ID);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.user_deptcode)) {
//            userInfoBean.user_deptcode = BuildCache.getAppConfigBean(context).userInfoBean.user_deptcode;
//        } else {
//            userInfoBean.user_deptcode = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_DEPTCODE);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.name)) {
//            userInfoBean.name = BuildCache.getAppConfigBean(context).userInfoBean.name;
//        } else {
//            userInfoBean.name = PreferencesUtils.getPfValue(Constants.ACCOUNT_NAME_KEY);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.sex)) {
//            userInfoBean.sex = BuildCache.getAppConfigBean(context).userInfoBean.sex;
//        } else {
//            userInfoBean.sex = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_SEX);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.score)) {
//            userInfoBean.score = BuildCache.getAppConfigBean(context).userInfoBean.score;
//        } else {
//            userInfoBean.score = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_SCORE);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.area_code)) {
//            userInfoBean.area_code = BuildCache.getAppConfigBean(context).userInfoBean.area_code;
//        } else {
//            userInfoBean.area_code = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_AREACODE);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).userInfoBean.qq)) {
//            userInfoBean.qq = BuildCache.getAppConfigBean(context).userInfoBean.qq;
//        } else {
//            userInfoBean.qq = PreferencesUtils.getPfValue(Constants.ACCOUNT_USER_QQ);
//        }
//        return userInfoBean;
//    }

    public static String getAppId(Context context) {
        return BuildCache.getAppConfigBean(context).appInfo.app_id;
    }

    public static AppInfoBean getAppInfo(Context context) {
        if (appInfoBean != null)
            return appInfoBean;
        appInfoBean = new AppInfoBean();

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_dept_areacode)) {
            appInfoBean.app_dept_areacode = BuildCache.getAppConfigBean(context).appInfo.app_dept_areacode;
        } else {
            appInfoBean.app_dept_areacode = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORG_AREACODE);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_deptid)) {
            appInfoBean.app_deptid = BuildCache.getAppConfigBean(context).appInfo.app_deptid;
        } else {
            appInfoBean.app_deptid = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORGID);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_deptname)) {
            appInfoBean.app_deptname = BuildCache.getAppConfigBean(context).appInfo.app_deptname;
        } else {
            appInfoBean.app_deptname = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORG_NAME);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_id)) {
            appInfoBean.app_id = BuildCache.getAppConfigBean(context).appInfo.app_id;
        } else {
            appInfoBean.app_id = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ID);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_orghxid)) {
            appInfoBean.app_orghxid = BuildCache.getAppConfigBean(context).appInfo.app_orghxid;
        } else {
            appInfoBean.app_orghxid = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_ORG_HXID);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_platcode)) {
            appInfoBean.app_platcode = BuildCache.getAppConfigBean(context).appInfo.app_platcode;
        } else {
            appInfoBean.app_platcode = PreferencesUtils.getPfValue(Constants.ACCOUNT_PLAT_CODE);
        }

        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appInfo.app_version)) {
            appInfoBean.app_version = BuildCache.getAppConfigBean(context).appInfo.app_version;
        } else {
            appInfoBean.app_version = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_VERSION);
        }
        return appInfoBean;
    }

//    public static AppIndexInfo getAppIndexInfo(Context context) {
//        if (appIndexBean != null)
//            return appIndexBean;
//        appIndexBean = new AppIndexInfo();
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appIndexInfo.app_url)) {
//            appIndexBean.app_url = BuildCache.getAppConfigBean(context).appIndexInfo.app_url;
//        } else {
//            appIndexBean.app_url = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_INDEX);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appIndexInfo.version_number)) {
//            appIndexBean.version_number = BuildCache.getAppConfigBean(context).appIndexInfo.version_number;
//        } else {
//            appIndexBean.version_number = PreferencesUtils.getPfValue(Constants.HTML_VERSION);
//        }
//
//        //新接口数据
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appIndexInfo.app_index)) {
//            appIndexBean.app_index = BuildCache.getAppConfigBean(context).appIndexInfo.app_index;
//        } else {
//            appIndexBean.app_index = PreferencesUtils.getPfValue(Constants.ACCOUNT_APP_INDEX);
//        }
//
//        if (!TextUtils.isEmpty(BuildCache.getAppConfigBean(context).appIndexInfo.app_versionnum)) {
//            appIndexBean.app_versionnum = BuildCache.getAppConfigBean(context).appIndexInfo.app_versionnum;
//        } else {
//            appIndexBean.app_versionnum = PreferencesUtils.getPfValue(Constants.HTML_VERSION);
//        }
//        return appIndexBean;
//    }

    public static String getUserZhenCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_zhencode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_ZhenCode);
        }
        return token;
    }

    public static String getUserZhenName() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_zhenname;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_Zhen_Name);
        }
        return token;
    }

    public static String getUserCunCode() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_cuncode;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_CunCode);
        }
        return token;
    }

    public static String getUserCunName() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_cunname;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_CunName);
        }
        return token;
    }

    public static String getUserSheid() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_sheid;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_SheId);
        }
        return token;
    }

    public static String getUserSheName() {
        String token = BuildCache.getAppConfigBean(BaseApplication.mInstance).userInfoBean.user_shename;
        if (TextUtils.isEmpty(token)) {
            token = PreferencesUtils.getPfValue(Constants.ACCOUNT_User_ShenName);
        }
        return token;
    }

    public static void clearEquipmentMap() {
        equipmentMap.clear();
    }

    public static String getPhoneGapUserinfo() {
        String info;
        JSONObject js = new JSONObject();
        Context context = BaseApplication.mInstance;
        try {
            js.put("name", getName(context));
            js.put("logo", getMyLogo(context));
            js.put("uuid", getMyUserid(context));
            js.put("isfarmer", MyInfoUtil.getIsFarmer());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        info = js.toString();
        return info;

    }

    /* 保存用户签到定位信息 */
    public static void setUserSignInfo(BUserSignInfo bUserSignInfo) {
        if (bUserSignInfo == null)
            return;
        if (!TextUtils.isEmpty(bUserSignInfo.signon_time)) {
            PreferencesUtils.setPfValue(Constants.SIGN_SIGNON_TIME, bUserSignInfo.signon_time);
        }
        if (!TextUtils.isEmpty(bUserSignInfo.signout_time)) {
            PreferencesUtils.setPfValue(Constants.SIGN_SIGNOUT_TIME, bUserSignInfo.signout_time);
        }
        if (!TextUtils.isEmpty(bUserSignInfo.class_date)) {
            PreferencesUtils.setPfValue(Constants.SIGN_CLASS_DATE, bUserSignInfo.class_date);
        }
        if (!TextUtils.isEmpty(bUserSignInfo.signOnFlag)) {
            PreferencesUtils.setPfValue(Constants.SIGN_ON_STATUS, bUserSignInfo.signOnFlag);
        }
        if (!TextUtils.isEmpty(bUserSignInfo.signOutFlag)) {
            PreferencesUtils.setPfValue(Constants.SIGN_OUT_STATUS, bUserSignInfo.signOutFlag);
        }
        if (!TextUtils.isEmpty(bUserSignInfo.trail_interval)) {
            PreferencesUtils.setPfValue(Constants.SIGN_TRAIL_INTERVAL, bUserSignInfo.trail_interval);
        }
    }

    /* 获取签到定位相关参数 */
    public static String getSignon_time() {
        return PreferencesUtils.getPfValue(Constants.SIGN_SIGNON_TIME);
    }

    public static String getSignout_time() {
        return PreferencesUtils.getPfValue(Constants.SIGN_SIGNOUT_TIME);
    }

    public static String getSignOnStatus() {
        return PreferencesUtils.getPfValue(Constants.SIGN_ON_STATUS);
    }

    public static String getSignOutStatus() {
        return PreferencesUtils.getPfValue(Constants.SIGN_OUT_STATUS);
    }

    public static String getClassDate() {
        return PreferencesUtils.getPfValue(Constants.SIGN_CLASS_DATE);
    }

    public static String getTrail_interval() {
        return PreferencesUtils.getPfValue(Constants.SIGN_TRAIL_INTERVAL);
    }

    /**
     * 设备是否绑定
     *
     * @return
     */
    public static boolean isDevicesBind() {
        Equipment equipment = equipmentMap.get(imei);
        if (equipment == null) { //没有在列表中找到该设备imei值
            return false; //未绑定设备
        } else {
            return true; //绑定设备
        }

    }
}
